package com.smart.sso.server.controller.admin;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smart.mvc.model.Result;
import com.smart.mvc.validator.Validator;
import com.smart.mvc.validator.annotation.ValidateParam;
import com.smart.sso.server.common.LoginUser;
import com.smart.sso.server.controller.common.BaseController;
import com.smart.sso.server.log.SystemControllerLog;
import com.smart.sso.server.model.App;
import com.smart.sso.server.model.UserRole;
import com.smart.sso.server.service.RoleAppService;
import com.smart.sso.server.service.UserRoleService;
import com.smart.sso.server.util.OperationUtils;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@Api(tags = "用户角色与应用关系管理")
@Controller
@RequestMapping("/admin/roleApp")
public class RoleAppController extends BaseController{
	@Resource
	private RoleAppService roleAppService;
	
	@Resource
	private UserRoleService userRoleService;
	
	@ApiOperation("初始页")
	@RequestMapping(method = RequestMethod.GET)
	public String editApp(@ApiParam(value = "角色id" , required = true) 
	@ValidateParam({Validator.NOT_BLANK}) Integer roleId , Model model , HttpServletRequest request) {
		model.addAttribute("roleId" , roleId);
		//model.addAttribute("appList" , roleAppService.findByRoleId(roleId));
		LoginUser curUser = (LoginUser)request.getSession().getAttribute("CURRENT_USER");
		List<UserRole> ls = userRoleService.findByUserRoleId(curUser.getUserId(), null);
		int curRoleId = ls.get(0).getRoleId();
		List<App> selectedApps = roleAppService.findByRoleId(roleId);
		ArrayList<Integer> appIds = new ArrayList();
		for(int i =0 ; i<selectedApps.size() ; i++) {
			appIds.add(selectedApps.get(i).getId());
		}
		model.addAttribute("appIds", appIds);
		return "/admin/roleApp";
	}
	
	@ApiOperation("列表")
	@RequestMapping(value = "/list" , method = RequestMethod.GET)
	public @ResponseBody Result list(
			@ApiParam(value = "roleId" , required = true) @ValidateParam({Validator.NOT_BLANK}) Integer roleId,
			HttpServletRequest request,
			Model model) {
		LoginUser curUser = (LoginUser)request.getSession().getAttribute("CURRENT_USER");
		List<UserRole> ls = userRoleService.findByUserRoleId(curUser.getUserId(), null);
		int curRoleId = ls.get(0).getRoleId();
		return Result.createSuccessResult().setData(roleAppService.findByRoleId(curRoleId));
	}
	
	@ApiOperation("角色授予应用提交")
	@RequestMapping(value = "/save" , method = RequestMethod.POST)
	@SystemControllerLog(operationId = OperationUtils.operation_role_app_authorize ,description = "角色应用授权")
	public @ResponseBody Result save(
			@ApiParam(value="角色id" , required = true) @ValidateParam({Validator.NOT_BLANK}) Integer roleId,
			@ApiParam(value = "ids", required = true) @ValidateParam({ Validator.NOT_BLANK }) String ids) {
		roleAppService.allocate(roleId, getAjaxIds(ids));
		return Result.createSuccessResult().setMessage("应用准入授权成功");
	}
}
